﻿function Spiffy_PasswordReset() {
    var instance = [];
    this.singletonInstance = null;

    var getInstance = function () {
        if (!this.singletonInstance) {
            this.singletonInstance = createInstance();
        }
        return this.singletonInstance;
    }

    var createInstance = function () {
        return {
            add: function (options) {
                var obj = new Object

                var defaults = {
                    moduleId: -1,
                    userId: -1,
                    unknownException:'',
                    controlPath:'',
                    passwordTooShort:'',
                    passwordsNotMatch:'',
                    passwordLength:0
                }

                var options = jQuery.extend({}, defaults, options);

                obj.moduleId = options.moduleId;
                obj.Init = function () {
                    jQuery(document).ready(function () {
                        jQuery("input[id$='txtConfirmPassword']").bind('keypress', function (e) {
                            if (e.keyCode == 13) {
                                this.resetPassword();
                                return false;
                            }
                        });
                    });
                }
                obj.resetPassword = function () {
                    this.toggleLoader();

                    if (this.validatePasswords()) {
                        $.ajax({
                            url: options.controlPath+"PasswordReset.ashx",
                            data: "method=resetPassword&UserId=" + options.userId + "&currentPassword=" + jQuery("input[id$='txtCurrentPassword']").val() + "&newPassword=" + jQuery("input[id$='txtNewPassword']").val() + "&confirmPassword=" + jQuery("input[id$='txtConfirmPassword']").val(),
                            type: "POST",
                            cache: false,
                            context:this,
                            dataType: "json",
                            success: function (data, status) {
                                this.toggleLoader();
                                this.clearPasswords();
                                if (data != null) {
                                    if (data.isBad == "True") {
                                        jQuery(".message").removeClass("messagePositive").addClass("messageNegative");
                                        jQuery(".message").html(data.msg);
                                    } else {
                                        jQuery(".message").removeClass("messageNegative").addClass("messagePositive");
                                        jQuery(".message").html(data.msg);
                                    }

                                } else {
                                    jQuery(".message").removeClass("messagePositive").addClass("messageNegative");
                                    jQuery(".message").html(options.unknownException);
                                }


                            },
                            error: function (msg) {
                                this.toggleLoader();
                                jQuery(".message").removeClass("messagePositive").addClass("messageNegative");
                                jQuery(".message").html(options.unknownException);
                                this.clearPasswords();
                            }
                        });
                    }
                }

                obj.toggleLoader = function () {
                    if (jQuery("#btnResetPassword").attr("disabled") == "disabled") {
                        jQuery("#resetPassword_ajaxLoader").show();
                        jQuery("#btnResetPassword").attr("disabled", "disabled");
                    } else {
                        jQuery("#resetPassword_ajaxLoader").hide();
                        jQuery("#btnResetPassword").attr("disabled", "");

                    }
                }

                obj.clearPasswords = function () {
                    jQuery("input[id$='txtCurrentPassword']").val("");
                    jQuery("input[id$='txtNewPassword']").val("");
                    jQuery("input[id$='txtConfirmPassword']").val("");
                }

                obj.validatePasswords = function () {
                    if (jQuery("input[id$='txtNewPassword']").val() != jQuery("input[id$='txtConfirmPassword']").val() || jQuery("input[id$='txtNewPassword']").val() == "") {
                        jQuery(".message").removeClass("messagePositive").addClass("messageNegative");
                        jQuery(".message").html(options.passwordsNotMatch);
                        this.toggleLoader();
                        this.clearPasswords();
                        return false;
                    } else if (jQuery("input[id$='txtNewPassword']").val().length < options.passwordLength) {
                        jQuery(".message").removeClass("messagePositive").addClass("messageNegative");
                        jQuery(".message").html(options.passwordTooShort);
                        this.toggleLoader();
                        this.clearPasswords();
                        return false;
                    } else {
                        return true;
                    }
                }



                instance.push(obj);
                return instance;
            },
            instance: function (id) {
                for (i = 0; i < instance.length; i++) {
                    if (instance[i].moduleId == id) {
                        return instance[i];
                    }
                }
            },
            instances: function () {
                return instance;
            }
        }
    }
    return getInstance();
}